#include "SphereMap.h"

SphereMap::SphereMap(){

	size = -1;
	texId = -1;
}

SphereMap::~SphereMap(){

	glDeleteTextures(1, &texId);
}

unsigned char * SphereMap::loadPPM(const char * const fn, unsigned int& w, unsigned int& h){

	char head[70];
	  int i,j;
	  int d;
	  GLubyte * img = NULL;

	  FILE * f = fopen(fn, "r");

	  if(f==NULL){
		fprintf(stderr,"Error in function readPPM : %s doesn't exist\n",fn);
		exit(EXIT_FAILURE);
	  }
	  fgets(head,70,f);

	  if(!strncmp(head, "P6", 2)){
		i=0;
		j=0;
		while(i<3){
		  fgets(head,70,f);

		  if(head[0] == '#'){
		continue;
		  }
		  if(i==0)
		i += sscanf(head, "%d %d %d", &w, &h, &d);
		  else
		if(i==1)
		  i += sscanf(head, "%d %d", &h, &d);
		else
		  if(i==2)
			i += sscanf(head, "%d", &d);
		}

		img = (GLubyte*) malloc((size_t)(w) * (size_t)(h) * 3 * sizeof(GLubyte));
		if(img==NULL){
		  fclose(f);
		  exit(EXIT_FAILURE);
		}

		fread(img, sizeof(GLubyte), (size_t)w*(size_t)h*3,f);
		fclose(f);
	  }
	  else{
		fclose(f);
		fprintf(stderr,"Error in function readPPM : %s isn't a PPM file\n",fn);
	  }
	  return img;

}

void SphereMap::draw(){

	glDisable(GL_FOG);
    glDisable(GL_LIGHTING);
	glEnable (GL_TEXTURE_CUBE_MAP);

  	glColor3f(1.0f, 1.0f, 1.0f);
	static GLfloat xPlane[] = { 1.0f, 0.0f, 0.0f, 0.0f };
	static GLfloat yPlane[] = { 0.0f, 1.0f, 0.0f, 0.0f };
	static GLfloat zPlane[] = { 0.0f, 0.0f, 1.0f, 0.0f };

	glEnable(GL_TEXTURE_GEN_S);
	glEnable(GL_TEXTURE_GEN_T);
	glEnable(GL_TEXTURE_GEN_R);
	glEnable(GL_TEXTURE_CUBE_MAP);

	glBindTexture(GL_TEXTURE_CUBE_MAP, texId);

	glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
	glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
	glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);

	glTexGenfv(GL_S, GL_OBJECT_PLANE, xPlane);
	glTexGenfv(GL_T, GL_OBJECT_PLANE, yPlane);
	glTexGenfv(GL_R, GL_OBJECT_PLANE, zPlane);

    glPushMatrix();
        glRotatef(-45.f, 1.f, 0.f, 0.f);
        glutSolidSphere (size, SUBDIVISION, SUBDIVISION);
    glPopMatrix();

	glDisable(GL_TEXTURE_CUBE_MAP);
	glDisable(GL_TEXTURE_GEN_S);
	glDisable(GL_TEXTURE_GEN_T);
	glDisable(GL_TEXTURE_GEN_R);

    glDisable (GL_TEXTURE_CUBE_MAP);
    glEnable(GL_LIGHTING);
	glEnable(GL_FOG);
}

void SphereMap::paramSphereMap(){

	  glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	  glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	  glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	  glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	  glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);

	  unsigned int tmpwidth, tmpheight;
	  unsigned char * image = loadPPM("resources/textures/greenhill_negative_x.ppm",tmpwidth,tmpheight);
	  if(image==0){
		std::cerr << "Erreur au chargement le l'image" << std::endl;
		exit(0);
	  }

	  glTexImage2D (GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
			0, GL_RGBA,
			tmpwidth, tmpheight, 0, GL_RGB,
			GL_UNSIGNED_BYTE, image);

	  delete[] image;

	  image = loadPPM("resources/textures/greenhill_negative_y.ppm",tmpwidth,tmpheight);
	  if(image==0){
		std::cerr << "Erreur au chargement le l'image" << std::endl;
		exit(0);
	  }

	  glTexImage2D (GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
			0, GL_RGBA,
			tmpwidth, tmpheight, 0, GL_RGB,
			GL_UNSIGNED_BYTE, image);


	  delete[] image;

	  image = loadPPM("resources/textures/greenhill_negative_z.ppm",tmpwidth,tmpheight);
	  if(image==0){
		std::cerr << "Erreur au chargement le l'image" << std::endl;
		exit(0);
	  }

	  glTexImage2D (GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
			0, GL_RGBA,
			tmpwidth, tmpheight, 0, GL_RGB,
			GL_UNSIGNED_BYTE, image);


	  delete[] image;

	  image = loadPPM("resources/textures/greenhill_positive_x.ppm",tmpwidth,tmpheight);
	  if(image==0){
		std::cerr << "Erreur au chargement le l'image" << std::endl;
		exit(0);
	  }

	  glTexImage2D (GL_TEXTURE_CUBE_MAP_POSITIVE_X,
			0, GL_RGBA,
			tmpwidth, tmpheight, 0, GL_RGB,
			GL_UNSIGNED_BYTE, image);


	  delete[] image;

	  image = loadPPM("resources/textures/greenhill_positive_y.ppm",tmpwidth,tmpheight);
	  if(image==0){
		std::cerr << "Erreur au chargement le l'image" << std::endl;
		exit(0);
	  }

	  glTexImage2D (GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
			0, GL_RGBA,
			tmpwidth, tmpheight, 0, GL_RGB,
			GL_UNSIGNED_BYTE, image);


	  delete[] image;

	  image = loadPPM("resources/textures/greenhill_positive_z.ppm",tmpwidth,tmpheight);
	  if(image==0){
		std::cerr << "Erreur au chargement le l'image" << std::endl;
		exit(0);
	  }

	  glTexImage2D (GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
			0, GL_RGBA,
			tmpwidth, tmpheight, 0, GL_RGB,
			GL_UNSIGNED_BYTE, image);


	  delete[] image;

	glBindTexture (GL_TEXTURE_CUBE_MAP, 0);
}

